package edu.gdin.ecommerce.action;

import java.io.IOException;
import java.util.Date;
import java.util.List;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import net.sf.json.JSONArray;
import net.sf.json.JsonConfig;

import org.apache.struts2.ServletActionContext;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;

import com.opensymphony.xwork2.ActionSupport;

import edu.gdin.ecommerce.model.CardIssuer;
import edu.gdin.ecommerce.service.ICardIssuerService;
import edu.gdin.ecommerce.util.DateUtil;
import edu.gdin.ecommerce.util.Pager;

@Controller("issuerAction")
@Scope("prototype")
public class CardIssuerAction extends ActionSupport{
	
	private String msgReturn;
	private CardIssuer issuer = new CardIssuer();
	private List<CardIssuer> issuers;
	private Integer currentPage;
	private Pager pager;
	
	private Integer status;
	
	private ICardIssuerService cardIssuerService;
	
	private String result;
	
	
	public String getMsgReturn() {
		return msgReturn;
	}
	public void setMsgReturn(String msgReturn) {
		this.msgReturn = msgReturn;
	}
	public CardIssuer getIssuer() {
		return issuer;
	}
	public void setIssuer(CardIssuer issuer) {
		this.issuer = issuer;
	}
	public String getResult() {
		return result;
	}
	public void setResult(String result) {
		this.result = result;
	}
	
	public ICardIssuerService getService() {
		return cardIssuerService;
	}
	
	@Resource()
	public void setService(ICardIssuerService service) {
		this.cardIssuerService = service;
	}
	public List<CardIssuer> getIssuers() {
		return issuers;
	}
	public void setIssuers(List<CardIssuer> issuers) {
		this.issuers = issuers;
	}
	
	public Integer getCurrentPage() {
		return currentPage;
	}
	public void setCurrentPage(Integer currentPage) {
		this.currentPage = currentPage;
	}
	
	public Pager getPager() {
		return pager;
	}
	public void setPager(Pager pager) {
		this.pager = pager;
	}
	
	public Integer getStatus() {
		return status;
	}
	public void setStatus(Integer status) {
		this.status = status;
	}
	public String addIssuerAction(){
		
		if (cardIssuerService.isExist(issuer.getName())){
			this.addFieldError("error", "此发卡商户已存在");
			return "issuerAdd";
		} 
		try{
			issuer.setCreateTime(new Date());
			cardIssuerService.addCardIssuer(issuer);
			setMsgReturn("success");
			return this.queryIssuerAction();
		}catch(Exception e ){
			this.addFieldError("error", "插入出错,请重试");
			return "issuerAdd";
		}
	}
	
	public String queryIssuerAction(){
		int cPage = currentPage == null ? 1 : currentPage;
		setPager(cardIssuerService.queryIssuers(issuer, cPage));
		return "issuerList";
	}
	
	public String updateStatusAction(){
		Integer	id = issuer.getCardIssuerId();
		CardIssuer issuer = cardIssuerService.getIssuerById(id);
		if(issuer!=null){
			issuer.setStatus(issuer.getStatus()==0 ? 1 : 0);
			cardIssuerService.udpateIssuer(issuer);
			setMsgReturn("updateSuccess");
		}
		
		return queryIssuerAction();
	}
	
	
	public String getFlagById() {
		this.issuer = this.cardIssuerService.getIssuerById(this.issuer.getCardIssuerId());
		HttpServletResponse response = ServletActionContext.getResponse();
		try {
			response.getWriter().write(this.issuer.getCardFlag());
		} catch (IOException e) {
			e.printStackTrace();
		}
		return null;
	}
	
	
	public String getAllIssuersAjax(){
		issuers = this.cardIssuerService.getAllIssuer();
		JsonConfig cfg = new JsonConfig();
		cfg.setExcludes(new String[]{"registerNo","authKey","mobile","tel","address","cardFlag","status","createTime","updateTime","cardIssuerDesc"});
		JSONArray array = JSONArray.fromObject(issuers, cfg);
		result = array.toString();
		return SUCCESS;
	}
}
